package cn.itheima.web.action;


import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Transaction;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.xwork2.ActionSupport;

import cn.itheima.domain.Customer;
import cn.itheima.service.CustomerService;
import cn.itheima.service.impl.CustomerServiceImpl;
import cn.itheima.utils.HibernateUtils;
public class CustomerAction extends ActionSupport {
	private CustomerService cs = new CustomerServiceImpl();
	private  Customer  c;
	
	public Customer getC() {
		return c;
	}

	public void setC(Customer c) {
		this.c = c;
	}

	public String list() throws Exception {
	
		List<Customer> list = cs.getAll();
		//5 将返回的list放入request域.转发到list.jsp显示
		ServletActionContext.getRequest().setAttribute("list", list);
		return "list";
	}
	
	public String add() throws Exception {
		cs.save(c);
		return "add";
	}
	
	public String delect() throws Exception {
		 String id=ServletActionContext.getRequest().getParameter("cust_id");
		 long cid=Integer.parseInt(id);
		 cs.delect(cid);
		 return "delect";
	}
	
	
	public String update() throws Exception {
		 String id=ServletActionContext.getRequest().getParameter("cust_id");
		 
		 long  cid=Integer.parseInt(id);
		 List<Customer>  update=new  ArrayList<Customer>();
		
		 update.add(cs.getById(cid));
		 
		 ServletActionContext.getRequest().setAttribute("update",update);
		 
		 return "update";
	}
	
	public String update_success() throws Exception {
		cs.update(c);
		 return "update_success";
	}

	



	
	
}
